<template>
  <Modal
    title="编辑"
    :value="value"
    :mask-closable="true"
    :footer-hide="true"
    @input="$emit('input', $event)"
  >
    <Form
      ref="formTybkCorporate"
      :model="formTybkCorporate"
      :rules="ruleValidate"
      label-position="right"
      :label-width="80"
    >
      <FormItem label="员工姓名" prop="staff_id">
        <Input type="text" v-model="formTybkCorporate.staff_id" placeholder="员工姓名"/>
      </FormItem>
      <FormItem label="客户姓名" prop="cooperation_name">
        <Input type="text" v-model="formTybkCorporate.cooperation_name" placeholder="客户姓名"/>
      </FormItem>
      <FormItem label="公司名称" prop="corporate_name">
        <Input type="text" v-model="formTybkCorporate.corporate_name" placeholder="公司名称"/>
      </FormItem>
      <FormItem label="联系人" prop="cooperation_contacts">
        <Input type="text" v-model="formTybkCorporate.cooperation_contacts" placeholder="联系人"/>
      </FormItem>
      <FormItem label="联系电话" prop="cooperation_phone">
        <Input type="text" v-model="formTybkCorporate.cooperation_phone" placeholder="联系电话"/>
      </FormItem>
      <FormItem label="邮箱" prop="corporate_mailbox">
        <Input type="text" v-model="formTybkCorporate.corporate_mailbox" placeholder="邮箱"/>
      </FormItem>
      <FormItem label="主营业务描述" prop="corporate_business_description">
        <Input type="text" v-model="formTybkCorporate.corporate_business_description" placeholder="主营业务描述"/>
      </FormItem>
      <FormItem label="需要的资质" prop="corporate_qualifications_required">
        <Input type="text" v-model="formTybkCorporate.corporate_qualifications_required" placeholder="需要的资质"/>
      </FormItem>
      <FormItem label="跟进记录" prop="corporate_record">
        <Input type="text" v-model="formTybkCorporate.corporate_record" placeholder="跟进记录"/>
      </FormItem>
      <FormItem label="是否成交(0未成交1已成交)" prop="corporate_Isdeal">
        <Select v-model="formTybkCorporate.corporate_Isdeal" style="width:200px">
          <Option value="0">未成交 </Option>
          <Option value="1">成交</Option>
        </Select>
      </FormItem>
      <FormItem label="合同金额" prop="corporate_contract_amount">
        <Input type="text" v-model="formTybkCorporate.corporate_contract_amount" placeholder="合同金额"/>
      </FormItem>
      <FormItem label="可推荐资质" prop="corporate_recommended_qualification">
        <Select v-model="formTybkCorporate.corporate_recommended_qualification" style="width:200px">
          <Option v-for="item in qualificationsList" :value="item.id" :key="item.id">{{ item.qualifications_name }}</Option>
        </Select>
      </FormItem>
      <FormItem label="回访记录" prop="corporate_return_visit">
        <Input type="text" v-model="formTybkCorporate.corporate_return_visit" placeholder="回访记录"/>
      </FormItem>
      <FormItem>
        <Button type="primary" @click="handleSubmit('formTybkCorporate')">提交</Button>
        <Button @click="handleReset('formTybkCorporate')" style="margin-left: 8px">重置</Button>
      </FormItem>
    </Form>
  </Modal>
</template>
<script>
import {getAllQualifications, update} from '@/api/corporate'
import { createModelObj, coverProps, coverFormProps } from '@/libs/util'

export default {
  name: 'Edit',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    tybkCorporate: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      qualificationsList:[],
      formTybkCorporate: {

        id: ''
        ,
        staff_id: ''
        ,
        cooperation_name: ''
        ,
        corporate_name: ''
        ,
        cooperation_contacts: ''
        ,
        cooperation_phone: ''
        ,
        corporate_mailbox: ''
        ,
        corporate_business_description: ''
        ,
        corporate_qualifications_required: ''
        ,
        corporate_record: ''
        ,
        corporate_Isdeal: null
        ,
        corporate_contract_amount: ''
        ,
        corporate_recommended_qualification: ''
        ,
        corporate_return_visit: ''
        ,
        deleted: 0
        ,
        create_time: ''
        ,
        modification_time: ''
        ,
        deletion_time: ''
      },
      ruleValidate: {

        cooperation_name: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        corporate_name: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 100, message: '长度最长为100', trigger: 'blur' }
        ]
        ,
        cooperation_contacts: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 55, message: '长度最长为55', trigger: 'blur' }
        ]
        ,
        cooperation_phone: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 20, message: '长度最长为20', trigger: 'blur' }
        ]
        ,
        corporate_mailbox: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 50, message: '长度最长为50', trigger: 'blur' }
        ]
        ,
        corporate_business_description: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        corporate_qualifications_required: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        corporate_record: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        corporate_contract_amount: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 10, message: '长度最长为10', trigger: 'blur' }
        ]
        ,
        corporate_recommended_qualification: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 255, message: '长度最长为255', trigger: 'blur' }
        ]
        ,
        corporate_return_visit: [
          { required: true, message: '该值不能为空', trigger: 'blur' },
          { type: 'string', max: 50, message: '长度最长为50', trigger: 'blur' }
        ]
        ,
      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          update(
            Object.assign({}, createModelObj(this.formTybkCorporate, 'tybkCorporate'))
          ).then(res => {
            if (res) {
              this.$emit('updateRow', coverProps(this.tybkCorporate, this.formTybkCorporate))
              this.$emit('input', false)
              this.handleReset(name)
            }
          })
        } else {
          this.$Message.error('验证失败!')
        }
      })
    },
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  },
  watch: {
    value: function (val, oldVal) {
      this.$refs.formTybkCorporate.resetFields()
      coverFormProps(this.formTybkCorporate, this.tybkCorporate)
      console.log(this.formTybkCorporate.corporate_contract_amount,'sss')
        getAllQualifications().then(res=>{
          this.qualificationsList=res
        })
    }
  }
}
</script>
